<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgpu - cubemap mix</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Cubemap Mix</span>
			</div>

			<small>
				Mixing two cube maps.<br/>
				Battle Damaged Sci-fi Helmet by <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a>
			</small>
		</div>
		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';
			import { mix, oscSine, time, pmremTexture, float } from 'three/tsl';

			import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

			import { Inspector } from 'three/addons/inspector/Inspector.js';

			let camera, scene, renderer;

			init();

			async function init() {

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
				camera.position.set( - 1.8, 0.6, 2.7 );

				scene = new THREE.Scene();

				const hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
				const cube1Texture = new HDRCubeTextureLoader()
					.setPath( './textures/cube/pisaHDR/' )
					.load( hdrUrls );

				cube1Texture.generateMipmaps = true;
				cube1Texture.minFilter = THREE.LinearMipmapLinearFilter;

				const cube2Urls = [ 'dark-s_px.jpg', 'dark-s_nx.jpg', 'dark-s_py.jpg', 'dark-s_ny.jpg', 'dark-s_pz.jpg', 'dark-s_nz.jpg' ];
				const cube2Texture = await new THREE.CubeTextureLoader()
					.setPath( './textures/cube/MilkyWay/' )
					.loadAsync( cube2Urls );

				cube2Texture.generateMipmaps = true;
				cube2Texture.minFilter = THREE.LinearMipmapLinearFilter;

				scene.environmentNode = mix( pmremTexture( cube2Texture ), pmremTexture( cube1Texture ), oscSine( time.mul( .1 ) ) );

				scene.backgroundNode = scene.environmentNode.context( {
					getTextureLevel: () => float( .5 )
				} );

				const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
				const gltf = await loader.loadAsync( 'DamagedHelmet.gltf' );

				scene.add( gltf.scene );

				renderer = new THREE.WebGPURenderer( { antialias: true } );

				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.toneMapping = THREE.LinearToneMapping;
				renderer.inspector = new Inspector();
				renderer.setAnimationLoop( render );
				document.body.appendChild( renderer.domElement );

				const controls = new OrbitControls( camera, renderer.domElement );
				controls.minDistance = 2;
				controls.maxDistance = 10;

				window.addEventListener( 'resize', onWindowResize );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function render() {

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
